package middleware

import (
	"gin/response"
	"gin/service"
	"github.com/gin-gonic/gin"
)

// Auth 中间件，检查token
func Auth() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		token := ctx.Request.Header.Get("token")
		if token == "" {
			// 必须携带token
			response.Fail(ctx, nil, "The request does not carry a token and has no permission to access!")
			return
		}

		// 验证token有效性
		check := service.CheckToken(token)
		if !check {
			response.Fail(ctx, nil, "Invalid token!")
		}
	}
}
